//
//  DDSortGoodsView.m
//  Dingdong
//
//  Created by 程强 on 16/11/28.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDSortGoodsView.h"

@interface DDSortGoodsView()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *sortTable;
@end

@implementation DDSortGoodsView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        backBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        [self addSubview:backBtn];
        [backBtn addTarget:self action:@selector(removeSortView) forControlEvents:UIControlEventTouchUpInside];
        [backBtn setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        
        UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, -44*3, frame.size.width, 44*3) style:UITableViewStylePlain];
        table.delegate = self;
        table.dataSource = self;
        [self addSubview:table];
        table.scrollEnabled = NO;
        self.sortTable = table;
        
        self.clipsToBounds = YES;
        
        weak_Self(self);
        [UIView animateWithDuration:0.3 animations:^{
            weakSelf.sortTable.frame = CGRectMake(0, 0, frame.size.width, 44*3);
        } completion:^(BOOL finished) {
            
        }];
    }
    return self;
}
- (void)setSortAry:(NSArray *)sortAry
{
    _sortAry = sortAry;
    [self.sortTable reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _sortAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"SortCell";
    
    DDSortCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    if (!cell) {
        cell = [[DDSortCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.sortLbl.text = _sortAry[indexPath.row];

    if (_sortAry[indexPath.row] == self.defaultSelectSort) {
        cell.sortLbl.highlighted = YES;
        cell.selectImg.highlighted = YES;
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDSortCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.sortLbl.highlighted = YES;
    cell.selectImg.highlighted = YES;
    [self removeSortView];
    if (self.delegate && [self.delegate respondsToSelector:@selector(sortView:didSelectIndex:)]) {
        [self.delegate sortView:self didSelectIndex:indexPath.row];
    }
}
- (void)removeSortView
{
    weak_Self(self);
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.sortTable.frame = CGRectMake(0, -44*3, self.bounds.size.width, 44*3);
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(sortViewRemoveFromSuperView)]) {
            [weakSelf.delegate sortViewRemoveFromSuperView];
        }
    }];
}
@end

@implementation DDSortCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        weak_Self(self);
        self.sortLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"",RGBColor(154, 154, 154),[UIFont systemFontOfSize:15.0],NSTextAlignmentLeft);
        }];
        _sortLbl.highlightedTextColor = kRedColor;
        [self.contentView addSubview:_sortLbl];
        [_sortLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakSelf.contentView);
            make.left.equalTo(weakSelf.contentView).offset(15);
            make.height.equalTo(@30);
            make.right.equalTo(weakSelf).offset(-50);
        }];
        
        self.selectImg = [[UIImageView alloc]init];
        _selectImg.highlightedImage = LoadImage(@"gouxuan");
        [self.contentView addSubview:_selectImg];
        [_selectImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakSelf.contentView);
            make.right.equalTo(weakSelf.contentView).offset(-25);
            make.width.equalTo(@16);
            make.height.equalTo(@13);
        }];
    }
    return self;
}

@end
